package com.estore.view;

import com.estore.model.Product;
import com.estore.service.ProductService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Comparator;

public class ConsoleShop {
    // 现有代码...
    private Map<Product, Integer> cart = new HashMap<>();
    private void showMenu() {
        System.out.println("\n=== 控制台商城 ===");
        System.out.println("1. 浏览所有商品");
        System.out.println("2. 自定义排序浏览商品"); // 新增选项
        System.out.println("3. 添加到购物车");
        System.out.println("4. 查看购物车");
        System.out.println("5. 结账");
        System.out.println("6. 退出");
        System.out.print("请选择操作: ");
    }

    public void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    browseProducts(null); // 默认排序
                    break;
                case 2:
                    showSortedProducts(); // 新增：自定义排序
                    break;
                case 3:
                    addToCart(scanner);
                    break;
                case 4:
                    viewCart();
                    break;
                case 5:
                    checkout();
                    break;
                case 6:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入。");
            }
        }
    }
    private void viewCart() {
        System.out.println("\n=== 购物车 ===");
        if (cart.isEmpty()) {
            System.out.println("购物车为空。");
            return;
        }

        double total = 0;
        System.out.println("购物车中的商品：");
        for (Map.Entry<Product, Integer> entry : cart.entrySet()) {
            Product product = entry.getKey();
            int quantity = entry.getValue();
            double subtotal = product.getPrice() * quantity;
            total += subtotal;
            System.out.printf("名称: %s, 单价: ¥%.2f, 数量: %d, 小计: ¥%.2f%n",
                    product.getName(), product.getPrice(), quantity, subtotal);
        }
        System.out.printf("总计: ¥%.2f%n", total);
    }

    private void addToCart(Scanner scanner) {
        System.out.print("请输入商品ID: ");
        String productId = scanner.nextLine();
        Product product = ProductService.getProductById(productId);
        if (product == null) {
            System.out.println("未找到该商品。");
            return;
        }

        System.out.print("请输入购买数量: ");
        int quantity = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        if (quantity <= 0 || quantity > product.getStock()) {
            System.out.println("库存不足或数量非法。");
            return;
        }

        cart.put(product, cart.getOrDefault(product, 0) + quantity);
        System.out.println("已添加到购物车。");
    }

    private void checkout() {
        System.out.println("\n=== 结账 ===");
        if (cart.isEmpty()) {
            System.out.println("购物车为空，无法结账。");
            return;
        }

        double total = 0;
        System.out.println("购物车中的商品：");
        for (Map.Entry<Product, Integer> entry : cart.entrySet()) {
            Product product = entry.getKey();
            int quantity = entry.getValue();
            double subtotal = product.getPrice() * quantity;
            total += subtotal;
            System.out.printf("名称: %s, 单价: ¥%.2f, 数量: %d, 小计: ¥%.2f%n",
                    product.getName(), product.getPrice(), quantity, subtotal);
        }

        System.out.printf("总计: ¥%.2f%n", total);
        cart.clear();
        System.out.println("结账成功，购物车已清空。");
    }





    private void showSortedProducts() {
        System.out.println("\n=== 选择排序方式 ===");
        System.out.println("1. 按价格从低到高");
        System.out.println("2. 按价格从高到低");
        System.out.println("3. 按库存从多到少");
        System.out.println("4. 返回主菜单");
        System.out.print("请选择排序方式: ");

        Scanner scanner = new Scanner(System.in);
        int sortChoice = scanner.nextInt();

        switch (sortChoice) {
            case 1:
                browseProducts(ProductService.BY_PRICE_ASC);
                break;
            case 2:
                browseProducts(ProductService.BY_PRICE_DESC);
                break;
            case 3:
                browseProducts(ProductService.BY_STOCK_DESC);
                break;
            case 4:
                return;
            default:
                System.out.println("无效选择，使用默认排序。");
                browseProducts(null);
        }
    }

    // 修改 browseProducts 方法以支持排序
    private void browseProducts(Comparator<Product> comparator) {
        List<Product> products = ProductService.getAllProducts();

        if (comparator != null) {
            // 使用自定义比较器排序
            products.sort(comparator);
            System.out.println("\n=== 排序后的商品列表 ===");
        } else {
            // 使用自然排序（Product 的 compareTo 方法）
            java.util.Collections.sort(products); // 修复：改为小写的 products
            System.out.println("\n=== 商品列表（按ID排序）===");
        }

        // 显示商品信息
        for (Product product : products) {
            System.out.printf("ID: %s, 名称: %s, 价格: ¥%.2f, 库存: %d%n",
                    product.getId(), product.getName(), product.getPrice(), product.getStock());
        }
    }

    public static void main(String[] args) {
        new ConsoleShop().start();
    }
    // 其他现有方法...
}